var Info = Vue.component('info', {
    data() {
        return {
            editStatus: true,       // 是否可编辑判断
            userInfoOld: {},        // 用户保存之前的值
            userInfo: {},           // 用户当前值
        }
    },

    template: `<div class="info__box">
                    <div class="info__con">
                        <h3 class="index__title info__title">公司信息</h3>
                        <div :class="['index__info', {input__border: !editStatus}]">
                            <div class="info__list">
                                <span>公司名称：</span>
                                <input placeholder="" v-model="userInfo.com_name" :disabled="editStatus">
                            </div>
                            <div class="info__list">
                                <span>业务类型：</span>
                                <input placeholder="" title="请在移动端上进行修改" :value="userInfo.out_type | filterBusiness" disabled>
                            </div>
                            <div class="info__list">
                                <span>公司座机：</span>
                                <input placeholder="" v-model="userInfo.com_tel" :disabled="editStatus">
                            </div>
                            <div class="info__list info__age">
                                <span>年龄范围：</span>
                                <input type="number" placeholder="" v-model="userInfo.ageStart" :disabled="editStatus">
                                —
                                <input type="number" placeholder="" v-model="userInfo.ageEnd" :disabled="editStatus">
                            </div>
                            <div class="info__list">
                                <span>公司传真：</span>
                                <input placeholder="" v-model="userInfo.com_fax" :disabled="editStatus">
                            </div>
                            <div class="info__list">
                                <span>坐标：</span>
                                <input placeholder="" :title="userInfo.comCity" v-model="userInfo.comCity" :disabled="editStatus">
                            </div>
                            <div class="info__list">
                                <span>公司地址：</span>
                                <input placeholder="" :title="userInfo.com_address" v-model="userInfo.com_address" :disabled="editStatus">
                            </div>
                            <div class="info__list">
                                <span>公司介绍：</span>
                                <input placeholder="" v-model="userInfo.detail" :disabled="editStatus">
                            </div>
                            <div class="info__list">
                                <span>公司所在地：</span>
                                <input placeholder="" :title="userInfo.comCity" v-model="userInfo.comCity" :disabled="editStatus">
                            </div>
                            <div class="info__list">
                                <span>客户满意度：</span>
                                <input placeholder="" v-model="userInfo.cus_like" :disabled="editStatus">
                            </div>
                            <div class="info__list">
                                <span>品质合格率：</span>
                                <input placeholder="" v-model="userInfo.quality" :disabled="editStatus">
                            </div>
                            <div class="info__list">
                                <span>合同履行率：</span>
                                <input placeholder="" v-model="userInfo.pact" :disabled="editStatus">
                            </div>
                        </div>
                        <div class="common__btn">
                            <div class="common__btn__box">
                                <span class="common__btn__sure" v-show="editStatus" @click="setEditFun">编辑</span>
                                <span class="common__btn__close" v-show="!editStatus" @click="setCancelFun">取消</span>
                                <span class="common__btn__sure" v-show="!editStatus" @click="updateUserInfo">保存设置</span>
                            </div>
                        </div>
                    </div>
                </div>`,

    methods: {
        setEditFun: function () {  // 编辑
            this.editStatus = !this.editStatus
        },
        setCancelFun: function () { // 取消
            var data = JSON.stringify(this.userInfoOld);
            this.userInfo = JSON.parse(data);
            this.editStatus = !this.editStatus;
        },
        setInfoFun: function () { // 保存设置
            this.editStatus = !this.editStatus
        },
        getUserInfo() {
            // 获取用户信息
            var self = this;
            $.ajax({
                type:"POST",
                url: _UrlPrefix + "/api/member/company/show",
                data: {
                    app_access: _AppAccess,
                    token: localStorage.getItem('token')
                },
                success:function(res){
                    if(0 == res.code) {
                        var data = JSON.stringify(res.data);
                        self.userInfo = JSON.parse(data);
                        self.userInfoOld = JSON.parse(data);
                    }
                }
            })
        },
        updateUserInfo() {
            // 修改用户信息
            var self = this;
            var imgArr = this.userInfo.legal_idcard_imgs.split('|');
            var ageRange = [this.userInfo.ageStart, this.userInfo.ageEnd].toString();
            $.ajax({
                type:"POST",
                url: _UrlPrefix + "/api/member/company/modify",
                data: {
                    app_access: _AppAccess,
                    token: localStorage.getItem('token'),
                    type: 2,
                    cate: 2,
                    "comInfo[facType]": this.userInfo.facType,
                    "comInfo[realname]": this.userInfo.legal_name,
                    "comInfo[sex]": this.userInfo.legal_sex,
                    "comInfo[mobile]": this.userInfo.legal_mobile,
                    "comInfo[idcard]": this.userInfo.legal_idcard,
                    "comInfo[sfzzm]": imgArr[0],
                    "comInfo[sfzfm]": imgArr[1],
                    "comInfo[sfzsc]": imgArr[2],
                    "comInfo[comName]": this.userInfo.com_name,
                    "comInfo[comTel]": this.userInfo.com_tel,
                    "comInfo[comFax]": this.userInfo.com_fax,
                    "comInfo[comAddress]": this.userInfo.com_address,
                    "comInfo[comCity]": this.userInfo.comCity,
                    "comInfo[cusLike]": this.userInfo.cus_like,
                    "comInfo[quality]": this.userInfo.quality,
                    "comInfo[pact]": this.userInfo.pact,
                    "comInfo[param]": this.userInfo.param,
                    "comInfo[param2][scale]": this.userInfo.param2.scale,
                    "comInfo[param2][totalSale]": this.userInfo.param2.totalSale,
                    "comInfo[param2][yearSale]": this.userInfo.param2.yearSale,
                    "comInfo[param2][truck]": this.userInfo.param2.truck,
                    "comInfo[param2][brand]": this.userInfo.param2.brand,
                    "comInfo[outTypeStr]": this.userInfo.outType,
                    "comInfo[ageRange]": ageRange,
                    "comInfo[detail]": this.userInfo.detail,
                    "comInfo[position]": this.userInfo.position,
                },
                success:function(res){
                    console.log(res)
                    if(0 == res.code) {
                        self.editStatus = !self.editStatus;
                    }
                }
            })
        }
    },

    filters: {
        filterBusiness(_type) {
            // 过滤业务类型
            if(_type) {
                var _typeArr = _type.split("||");
                var businessTypeArr = [             // 业务类型
                    { id: 116, label: '散批' },
                    { id: 117, label: '贴牌' },
                    { id: 118, label: '可做经销单' },
                    { id: 119, label: '来料加工' },
                    { id: 120, label: '可代理' },
                    { id: 121, label: '可加盟' },
                    { id: 122, label: '网店供货' },
                    { id: 123, label: '市场货' },
                    { id: 124, label: '公司货' },
                    { id: 125, label: '全精品' },
                    { id: 126, label: '半精品' },
                    { id: 127, label: '外销' },
                    { id: 128, label: '内销' }
                ];           
                var newType = businessTypeArr.filter(item => {
                    for(var i = 0;i < _typeArr.length;i++) {
                        if(_typeArr[i] == item.id) {
                            return item.label;
                        }
                    }
                })
                return newType.map(item => {
                    return item.label
                })
                // return newType.join(",");
            }
        }
    },

    mounted() {
        
    },

    created() {
        // 获取用户信息
        this.getUserInfo();
    },

})